package com.see.domain;

import java.math.BigDecimal;
import java.util.Comparator;

public class Order {
	
    private String product;
    private Price price;
    private long amount;
    private String user;
    private Operation type;
    private int id = 0;
    private boolean match = false;
    private BigDecimal matchprice = BigDecimal.ZERO;
    private String counterparty = "";
    
    public Order(String userName, String product, Price price, long amount, Operation type){
        this.user = userName;
        this.product = product;
        this.price = price;
        this.amount = amount;
        this.type = type;
    }

	
    public String getProduct() {
		return product;
	}

	public void setProduct(String product) {
		this.product = product;
	}

	public Price getPrice() {
		return price;
	}

	public void setPrice(Price price) {
		this.price = price;
	}

	public long getAmount() {
		return amount;
	}

	public void setAmount(long amount) {
		this.amount = amount;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public Operation getType() {
		return type;
	}

	public void setType(Operation type) {
		this.type = type;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public boolean isMatch() {
		return match;
	}

	public void setMatch(boolean match) {
		this.match = match;
	}
	
	public BigDecimal getMatchprice() {
		return matchprice;
	}


	public void setMatchprice(BigDecimal matchprice) {
		this.matchprice = matchprice;
	}


	public String getCounterparty() {
		return counterparty;
	}


	public void setCounterparty(String counterparty) {
		this.counterparty = counterparty;
	}

	@Override
	public boolean equals(Object other) {
		if (this == other)
			return true;
		if (other == null)
			return false;
		if (this.getClass() != other.getClass())
			return false;
		
		Order otherObj = (Order) other;
		if(!this.product.equals(otherObj.product))
			return false;
		if(this.amount != otherObj.amount)
			return false;
		
		if(!this.price.equals(otherObj.price))
			return false;
		
		return true;
	}


	private static int compareid(Order o1, Order o2){
    	if (o1.id == o2.id) {
            return 0;
        } else if (o1.id < o2.id) {
            return -1;
        } else if (o1.id > o2.id) {
            return 1;
        } else {
            return 0;
        }
    }
	
    
    @Override
	public String toString() {
		return type + " order: " + product + " amount= " + amount + " price= " + price + " of " + user;
	}


	public static class BuyOrder implements Comparator<Order> {
    	@Override
    	public int compare(Order o1, Order o2) {
    		int pricecompare = o1.price.compareTo(o2.price);
    		if (pricecompare == 0) {
    			return compareid(o1,(Order) o2);
    		}
    		return pricecompare*(-1);
    	}
    }
    
    public static class SellOrder implements Comparator<Order> {
    	@Override
    	public int compare(Order o1, Order o2) {
    		int pricecompare = o1.price.compareTo(o2.price);
    		if (pricecompare == 0) {
    			return compareid(o1,(Order) o2);
    		}
    		return pricecompare;
    	}
    }

    public static void main(String[] args) {}


}
